package com.yanqu.road.dataHelper.manager;


import com.yanqu.road.dataHelper.utils.ExcelData;
import com.yanqu.road.dataHelper.utils.TextData;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

//导出数据的
public class DataMgr {

    private static List<Map<String,String>> allData = new ArrayList<>();

    private static List<String> lineData = new ArrayList<>();

    private static Map<Integer,Map<String, UnionActivityConditionData>> unionConditionMap = new ConcurrentHashMap<>();
    private static Map<Integer,Map<Long, Map<String,String>>> userConditionMap = new ConcurrentHashMap<>();

    public synchronized static void addUnionData(Map<Integer,Map<String, UnionActivityConditionData>> addData){
        unionConditionMap.putAll(addData);
    }

    public static Map<Integer,Map<String, UnionActivityConditionData>> getUnionData(){
        return unionConditionMap;
    }

    public synchronized static void addUserConditionData(Map<Integer,Map<Long, Map<String,String>>> addData){
        userConditionMap.putAll(addData);
    }

    public static Map<Integer,Map<Long, Map<String,String>>> getUserConditionData(){
        return userConditionMap;
    }

    public synchronized static void addData(List<Map<String,String>> inData){
        allData.addAll(inData);
    }
    public static List<Map<String,String>> getData(){
        return allData;
    }

    public static void outExcel(String fileName){
        if(allData.size() == 0){
            System.err.println("excel no data");
            return;
        }
        List<String> keyList = new ArrayList<>(allData.get(0).keySet());
        try {
            ExcelData.outExcelData(fileName,keyList,allData);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void outExcel(String fileName,List<String> keyList){
        if(allData.size() == 0){
            return;
        }
        try {
            ExcelData.outExcelData(fileName,keyList,allData);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void makeExcelData2Txt(String title){
        makeTextData(title,allData);
        clearExcelData();
    }

    public static void clearExcelData(){
        allData = new ArrayList<>();
    }

    public static void clearTextData(){
        lineData = new ArrayList<>();
    }

    public static void makeTextData(String title,List<Map<String,String>> data){
        if(data.size() == 0){
            return;
        }
        List<String> keyList = new ArrayList<>(data.get(0).keySet());
        lineData.add(title);
        String keyStr = "";
        for(int i=0;i<keyList.size();i++){
            if(!keyStr.equals("")){
                keyStr += "    ";
            }
            keyStr += keyList.get(i);
        }
        lineData.add(keyStr);
        for(Map<String,String> one : data){
            String lineStr = "";
            for(int i=0;i<keyList.size();i++){
                if(!lineStr.equals("")){
                    lineStr += "    ";
                }
                lineStr += one.get(keyList.get(i));
            }
            lineData.add(lineStr);
        }
    }

    public static void addTextLine(String line){
        if(StringUtils.isNullOrEmpty(line)){
            return;
        }
        lineData.add(line);
    }

    public static void outText(String fileName){
        if(lineData.size() == 0){
            System.err.println("text no data");
            return;
        }
        try {
            TextData.outTextData(fileName,lineData);
            DataMgr.clearTextData();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
